<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity"
                 style="padding-bottom: 0px;">
          <!--          <el-form-item label="">-->
          <!--            <el-input v-model="params.entity.id"-->
          <!--                      placeholder="请输入"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="信息类型">-->
          <!--            <el-input v-model="params.entity.msgType"-->
          <!--                      placeholder="请输入信息类型 0-系统消息 1-订单消息 2-核销消息"/>-->

          <!--            <el-select v-model="params.entity.msgType" clearable placeholder="请选择信息类型">-->
          <!--              <el-option :label="'系统消息'" :value=0></el-option>-->
          <!--              <el-option :label="'订单消息'" :value=1></el-option>-->
          <!--              <el-option :label="'核销消息'" :value=2></el-option>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="信息内容">
            <el-input v-model="params.entity.msgContent"
                      placeholder="请输入信息内容"/>
          </el-form-item>
          <el-form-item label="是否推送" v-if="urlName.value == 'sysMsgList'">
            <!--            <el-input v-model="params.entity.delayFlag"-->
            <!--                      placeholder="请输入是否已推送 0-否  1-是"/>-->
            <el-select v-model="params.entity.delayFlag" clearable placeholder="请选择是否推送" style="width:160px">
              <el-option :label="'否'" :value=0></el-option>
              <el-option :label="'是'" :value=1></el-option>
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="推送用户 ,逗号隔开">-->
          <!--            <el-input v-model="params.entity.msgUser"-->
          <!--                      placeholder="请输入推送用户 ,逗号隔开"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="状态 0-停用 1-启用 2-备用">-->
          <!--            <el-input v-model="params.entity.statusFlag"-->
          <!--                      placeholder="请输入状态 0-停用 1-启用 2-备用"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="是否删除 0-否 1-是">-->
          <!--            <el-input v-model="params.entity.delFlag"-->
          <!--                      placeholder="请输入是否删除 0-否 1-是"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="创建人">-->
          <!--            <el-input v-model="params.entity.createUser"-->
          <!--                      placeholder="请输入创建人"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="创建时间">-->
          <!--            <el-input v-model="params.entity.createTime"-->
          <!--                      placeholder="请输入创建时间"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="修改时间">-->
          <!--            <el-input v-model="params.entity.updateTime"-->
          <!--                      placeholder="请输入修改时间"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="备注">-->
          <!--            <el-input v-model="params.entity.remark"-->
          <!--                      placeholder="请输入备注"/>-->
          <!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询
            </el-button>
            <el-button type="primary" :icon="Plus" @click="addDialog"  v-if="urlName.value == 'sysMsgList'">新增
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="500"
              :default-sort="{ prop: 'date', order: 'descending' }"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column fixed type="selection" width="55"/>-->
      <el-table-column prop="id" label="ID" width="55"/>
      <!--      <el-table-column prop="msgTypeName" label="信息类型"-->
      <!--                       show-overflow-tooltip/>-->
      <el-table-column prop="msgContent" label="信息内容"
                       show-overflow-tooltip/>
      <el-table-column prop="delayFlagName" label="是否推送" v-if="urlName.value == 'sysMsgList'" show-overflow-tooltip/>
      <el-table-column prop="delayTime" label="推送时间" width="200"/>
      <!--      <el-table-column prop="msgUser" label="推送用户 ,逗号隔开"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="statusFlag" label="状态 0-停用 1-启用 2-备用"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="delFlag" label="是否删除 0-否 1-是"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createUser" label="创建人"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createTime" label="创建时间"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="updateTime" label="修改时间"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="remark" label="备注" show-overflow-tooltip/>-->

      <el-table-column fixed="right" label="操作" width="200"
                       v-if="urlName.value == 'sysMsgList'" >
        <!--        <template #header>-->
        <!--          <el-input v-model="params.search" @keyup.enter.native="searchData"-->
        <!--                    placeholder="搜索角色名称/介绍"/>-->
        <!--        </template>-->
        <template #default="scope">
          <el-button link type="danger" :icon="Delete" size="small"
                     @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
          <el-button link type="primary" :icon="Edit" size="small"
                     @click="editDialog(scope.row)">编辑
          </el-button>
          <el-button link v-if="scope.row.delayFlag ==0" type="primary" :icon="Edit" size="small"
                     @click="delayOpen(scope.row)">推送
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="params.current"
                   v-model:page-size="params.size"
                   :page-sizes="[100, 200, 300, 400]"
                   layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"/>
  </el-card>

  <!-- 新增或者修改页面-->
  <RetailMsgAddOrEdit
      @closeEditDialog="closeEditDialog"
      :addOrEditVisible="addOrEditVisible"
      :ruleForm="ruleForm"
      :urlName="urlName"
      :addDataType="addDataType"></RetailMsgAddOrEdit>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import {onMounted, computed, reactive, ref} from 'vue'
import {Params, Result} from "@/apis/model/common/common.interfaces"
import {
  Delete,
  Edit,
  Search,
  Share,
  Upload,
  Plus,
  Setting
} from '@element-plus/icons-vue'
import RetailMsgAddOrEdit from './retailMsgAddOrEdit.vue'
import {RetailMsg}
  from "@/apis/model/msg/RetailMsgModel";
import {
  delayMsg,
  deleteRetailMsgById,
  retailMsgPage
} from "@/apis/http/msg/RetailMsgHttp";
import router from "@/router";

// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {}
})

const ruleForm = ref<RetailMsg>({} as RetailMsg)
const urlName = ref<String>('')
// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)
const addDataType = ref('add');

/**
 * 初始化数据
 */
const initList = () => {
  initUrl();
  loading.value = true
  retailMsgPage(params).then((res: Result) => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as RetailMsg
  addDataType.value = 'add'
}

//修改弹窗
const editDialog = (row:
                        RetailMsg
) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

// 关闭修改弹窗
const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm(
      '是否删除?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    deleteRetailMsgById(id).then((res: Result) => {
      const code = res.code;
      if (code == 200) {
        ElMessage({
          message: '删除数据成功！',
          type: 'success',
        })
        initList();
      } else {
        ElMessage.error('删除数据失败！');
      }
    })
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })
}

const delayOpen = (row: RetailMsg) => {
  ElMessageBox.confirm(
      '确认推送系统消息?', '推送', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
    delayMsg(row.id).then(res => {
      ElMessage({
        type: 'success',
        message: '推送成功',
      });
      initList()
    })
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })
}

const initUrl = () => {
  urlName.value = router.currentRoute.value.name||'';

  if (urlName.value == 'sysMsgList') {
    params.entity.msgType = 0
  } else if (urlName.value == 'shopMsg') {
    params.entity.msgType = 1;
    params.entity.delayFlag = 1;
  } else if (urlName.value == 'tixianMsg') {
    params.entity.msgType = 2;
    params.entity.delayFlag = 1;
  } else if (urlName.value == 'sysMsg') {
    params.entity.msgType = 0;
    params.entity.delayFlag = 1;
  }
}

onMounted(() => {
  urlName.value = router.currentRoute.value.name||'';
  initList()
})
</script>

<style>
</style>
